<?php
$contact_id = intval( dPgetParam( $_GET, 'contact_id', 0 ) );
$company_id = intval( dPgetParam( $_REQUEST, 'company_id', 0 ) );
$company_name = dPgetParam( $_REQUEST, 'company_name', null );

// check permissions for this record
$perms =& $AppUI->acl();
if (! ($canEdit = $perms->checkModuleItem( 'contacts', 'edit', $contact_id )) ) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}

// load the record data
$msg = '';
$row = new CContact();

$canDelete = $row->canDelete( $msg, $contact_id );
if($msg == $AppUI->_('contactsDeleteUserError', UI_OUTPUT_JS)) {
	$userDeleteProtect=true;
}

if ($view) {
	$view = ' readonly="true"';
}
else {
	unset($view);
}

if (!$row->load( $contact_id ) && $contact_id > 0) {
	$AppUI->setMsg( 'Contact' );
	$AppUI->setMsg( 'invalidID', UI_MSG_ERROR, true );
	$AppUI->redirect();
} else if ($row->contact_private && $row->contact_owner != $AppUI->user_id
	&& $row->contact_owner && $contact_id != 0) {
// check only owner can edit
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}

$company_detail = $row->getCompanyDetails();
$dept_detail = $row->getDepartmentDetails();
if ($contact_id == 0 && $company_id > 0) {
	$company_detail['company_id'] = $company_id;
	$company_detail['company_name'] = $company_name;
}
$birthday = isset($row->contact_birthday) && $row->contact_birthday != '0000-00-00' ?
	new CDate($row->contact_birthday) : null;

$q = new DBQuery();
$q->addTable('companies');
$q->addQuery('company_id, company_name');
$companies = arrayMerge( array('' => ''), $q->loadHashList() );

$contactTypeArr = dPgetSysVal('ContactType');
if (!$view) {
?>
<form name="ContactForm" action="?m=contacts" method="post">
	<input type="hidden" name="dosql" value="do_contact_aed" />
	<input type="hidden" name="contact_project" value="0" />
	<input type="hidden" name="contact_unique_update" value="<?=uniqid('')?>" />
	<input type="hidden" name="contact_id" value="<?=$contact_id?>" />
	<input type="hidden" name="contact_owner" value="<?=$row->contact_owner ? $row->contact_owner : $AppUI->user_id?>" />
<?
}
?>
<table border="0" cellpadding="8" cellspacing="0" width="400px">
<tr>
<td colspan="2" bgcolor="#E0E0E0"><b><?=$AppUI->_('Contact Information')?></td>
</tr>
<tr>
	<td colspan="2">
		<table border="0" cellpadding="0" cellspacing="4" width="100%">
		<tr>
			<td align="right"><?=$AppUI->_('First name')?></td>
			<td nowrap>
				<input type="text" class="text" name="contact_first_name"
				value="<?=@$row->contact_first_name?>" maxlength="50" style="width: 280px" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right"><?=$AppUI->_('Last name')?></td>
			<td nowrap>
				<input type="text" class="text" name="contact_last_name"
					value="<?=@$row->contact_last_name?>" maxlength="50" style="width: 280px" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right"><?=$AppUI->_('Display name')?></td>
			<td nowrap>
				<input type="text" class="text" name="contact_order_by"
					value="<?=@$row->contact_order_by?>" maxlength="50" style="width: 280px" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right"><?=$AppUI->_('Type')?></td>
			<td nowrap>
				<?=arraySelect($contactTypeArr,'contact_type'," id='idContact_type' class='text' style='width: 250px' ", @$row->contact_type)?>
				<?=$AppUI->_('Birthday')?>
				<input id="idBirthday" type="hidden" name="contact_birthday"
					value="<?=$birthday ? $birthday->format(FMT_DATETIME_MYSQL) : ''?>">
				<input id="idBirthdayD" type="text" class="text" name="contact_birthday_text"
					value="<?=$birthday ? $birthday->format($AppUI->getPref('SHDATEFORMAT')) : ''?>"
					maxlength="10" style="width: 64px" <?=$view?>/>
				<img id="idBirthdayB" class="ico" src="images/calendar.gif"/>
			</td>
		</tr>
		</table>
	</td>
</tr>
<tr>
	<td colspan="2" width="100%" align="center" style="padding-top: 0px">
	<table border="0" cellspacing="0" cellpadding="0" width="100%">
		<tr class="infotab">
			<td class="here" onclick="doSwitchTab(this)">Company</td>
			<td class="pad" width="4px">&nbsp;</td>
			<td class="tab" onclick="doSwitchTab(this)">Personal</td>
			<td class="pad" width="4px">&nbsp;</td>
			<td class="tab" onclick="doSwitchTab(this)">Others</td>
			<td class="pad" width="4px">&nbsp;</td>
			<td class="tab" onclick="doSwitchTab(this)">Comments</td>
			<td class="pad" width="100%">&nbsp;</td>
		</tr>
	</table>
	<div style="border: 1px solid #AAA; border-top: 0px; width: 380px; height: 165px; padding: 4px">
		<!-- Company -->
		<table id="idCompany" border="0" cellpadding="2" cellspacing="0" width="100%">
		<tr>
			<td align="right" width="80px"><?=$AppUI->_('Name')?></td>
			<td colspan="3" align="left">
			<?	if ($view) { ?>
				<input type="text" class="text" name="contact_company_name" value="<?=$company_detail['company_name']?>"
					maxlength="100" style="width: 280px" <?=$view?>/>
			<?	} else {
					echo arraySelect($companies, 'contact_company',
							'id="idContactCompany" size="1" class="text" style="width: 285px" onchange="doChangeCompany()"',
							$company_detail['company_id']);
				} ?>
			</td>
		</tr>
		<tr>
			<td align="right" width="80px"><?=$AppUI->_('Deparment')?></td>
			<td colspan="3" align="left" nowrap="nowrap">
				<input id="idDeptName" type="text" class="text" name="contact_department_name"
					value="<?=$dept_detail['dept_name']?>" maxlength="100"
					style="width: <?=$view ? 280 : 260?>px" readonly="true"/>
			<? if (!$view) { ?>
				<img class="ico clickable" src="images/treetable.gif" onclick="doShowDeptSelector()"/>
				<input id="idDeptId" type='hidden' name='contact_department' value='<?=$dept_detail['dept_id']?>'>
			<? } ?>
			</td>
		</tr>
		<tr>
			<td align="right" width="80px"><?=$AppUI->_('Position')?></td>
			<td colspan="3" align="left">
				<input type="text" class="text" name="contact_title" value="<?=@$row->contact_title?>"
					maxlength="50" style="width: 280px" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right" width="80px"><?=$AppUI->_('Occupation')?></td>
			<td colspan="3" align="left">
				<input type="text" class="text" name="contact_job" value="<?=@$row->contact_job?>"
					maxlength="100" style="width: 280px" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right" width="80px"><?=$AppUI->_('Email')?></td>
			<td colspan="3" align="left">
				<input type="text" class="text" name="contact_email" value="<?=@$row->contact_email?>"
					maxlength="255" style="width: 280px" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right" width="80px"><?=$AppUI->_('Phone')?></td>
			<td align="left">
				<input type="text" class="text" name="contact_phone" value="<?=@$row->contact_phone?>"
					maxlength="30" style="width: 120px" <?=$view?>/>
			</td>
			<td align="right"><?=$AppUI->_('Fax')?></td>
			<td align="left" width="120px">
				<input type="text" class="text" name="contact_fax" value="<?=@$row->contact_fax?>"
					maxlength="30" style="width: 120px" <?=$view?>/>
			</td>
			<td>&nbsp;</td>
		</tr>
		<tr><td colspan="100%"><img src="images/blank.gif" width="100%" height="0"></td></tr>
		</table>

		<!-- Personal info -->
		<table id="idPersonal" border="0" cellpadding="2" cellspacing="0" width="100%"
			style="display: none; visibility: hidden">
		<tr>
			<td align="right" width="80px" valign="top"><?=$AppUI->_('Address')?></td>
			<td colspan="3" align="left">
				<textarea class="text" name="contact_address1" style="width: 280px; height: 40px" <?=$view?>><?=@$row->contact_address1?></textarea>
			</td>
		</tr>
		<tr>
			<td align="right" width="80px"><?=$AppUI->_('City')?></td>
			<td colspan="3" align="left">
				<input type="text" class="text" name="contact_city" value="<?=@$row->contact_city?>"
					maxlength="30" style="width: 280px" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right" width="80px"><?=$AppUI->_('State')?></td>
			<td align="left">
				<input type="text" class="text" name="contact_state" value="<?=@$row->contact_state?>"
					maxlength="30" style="width: 120px" <?=$view?>/>
			</td>
			<td align="right"><?=$AppUI->_('Zip')?></td>
			<td colspan="3" align="left">
				<input type="text" class="text" name="contact_zip" value="<?=@$row->contact_zip?>"
					maxlength="11" style="width: 100px" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right"><?=$AppUI->_('Phone')?></td>
			<td align="left">
				<input type="text" class="text" name="contact_phone2" value="<?=@$row->contact_phone2?>"
					maxlength="30" style="width: 120px" <?=$view?>/>
			</td>
			<td align="right"><?=$AppUI->_('Mobile')?></td>
			<td align="left">
				<input type="text" class="text" name="contact_mobile" value="<?=@$row->contact_mobile?>"
					maxlength="30" style="width: 100px" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right" width="80px"><?=$AppUI->_('Email')?></td>
			<td colspan="3" align="left">
				<input type="text" class="text" name="contact_email2" value="<?=@$row->contact_email2?>"
					maxlength="255" style="width: 280px" <?=$view?>/>
			</td>
		</tr>
		<tr><td colspan="100%"><img src="images/blank.gif" width="100%" height="0"></td></tr>
		</table>

		<!-- Internet contact -->
		<table id="idOthers" border="0" cellpadding="2" cellspacing="0" width="100%"
			style="display: none; visibility: hidden">
		<tr>
			<td align="right" width="80px"><?=$AppUI->_('Homepage')?></td>
			<td colspan="3" align="left">
				<input type="text" class="text" name="contact_url" value="<?=@$row->contact_url?>"
					maxlength="255" style="width: 280px" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right" width="80px">Yahoo</td>
			<td align="left">
				<input type="text" class="text" name="contact_yahoo" value="<?=@$row->contact_yahoo?>"
					maxlength="255" style="width: 110px" <?=$view?>/>
			</td>
			<td align="right" width="40px">Skype</td>
			<td align="left">
				<input type="text" class="text" name="contact_skype" value="<?=@$row->contact_skype?>"
					maxlength="255" style="width: 110px" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right" width="80px">AOL</td>
			<td align="left">
				<input type="text" class="text" name="contact_aol" value="<?=@$row->contact_aol?>"
					maxlength="20" style="width: 110px" <?=$view?>/>
			</td>
			<td align="right">MSN</td>
			<td align="left">
				<input type="text" class="text" name="contact_msn" value="<?=@$row->contact_msn?>"
					maxlength="255" style="width: 110px" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right" width="80px">Jabber</td>
			<td align="left">
				<input type="text" class="text" name="contact_jabber" value="<?=@$row->contact_jabber?>"
					maxlength="255" style="width: 110px" <?=$view?>/>
			</td>
			<td align="right">ICQ</td>
			<td align="left">
				<input type="text" class="text" name="contact_icq" value="<?=@$row->contact_icq?>"
					maxlength="20" style="width: 110px" <?=$view?>/>
			</td>
		</tr>
		<tr><td colspan="100%"><img src="images/blank.gif" width="100%" height="0"></td></tr>
		</table>

		<!-- Comments -->
		<table id="idComments" border="0" cellpadding="2" cellspacing="0" width="100%"
			style="display: none; visibility: hidden">
		<tr>
			<td align="center">
				<textarea class="text" name="contact_notes" rows="9" style="width: 360px" <?=$view?>><?=@$row->contact_notes?></textarea>
			</td>
		</tr>
		</table>
	</div>
	</td>
</tr>
<tr>
	<td colspan="2" style="padding-top: 0px">
		<input id="idCP" type="checkbox" value="1" name="contact_private"
			<?=(@$row->contact_private ? 'checked="checked"' : '')?>/>
		<label for="idCP"><?=$AppUI->_('Private')?></label>
	</td>
</tr>

<tr>
	<td align="center" colspan="2" style="border-top: 1px solid #E0E0E0">
	<? if (!$view) { ?>
		<input type="submit" class="button" value="<?=$AppUI->_('Save')?>"/>
		<input type="button" class="button" value="<?=$AppUI->_('Cancel')?>" onClick="doCloseContact()"/>
	<? } else { ?>
		<input type="button" class="button" value="<?=$AppUI->_('Close')?>" onClick="doCloseContact()"/>
	<? } ?>
	</td>
</tr>
</table>
<? if (!$view) { ?>
</form>
<? } ?>
<div id="validation_rules" style="visibility: hidden; display: none">
	var fcontact_first_name = new LiveValidation('contact_first_name');
	fcontact_first_name.add( Validate.Presence);
	fcontact_first_name.add( Validate.Length, { maximum: 150 } );

	var fcontact_last_name = new LiveValidation('contact_last_name');
	fcontact_last_name.add( Validate.Presence);
	fcontact_last_name.add( Validate.Length, { maximum: 150 } );

	var fcontact_type = new LiveValidation('contact_type');
	fcontact_type.add( Validate.Presence);

	var fcontact_order_by = new LiveValidation('contact_order_by');
	fcontact_order_by.add( Validate.Presence);
</div>
<script type="text/javascript" language="javascript">
	eval($('validation_rules').innerHTML);
</script>